package com.huiquan.management.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.foundation.constant.RoleID;
import com.huiquan.foundation.constant.TabID;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.dao.PageRoleDao;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.management.domain.Page;

/**
 * ClassName:Constants Description:
 *
 * @author ChaoKai Wen email:wenchaokai@gmail.com
 * @Date 14/9/22
 * @since Ver 1.0
 */
@Service("pageRoleService")
public class PageRoleService extends BaseService {

	@Autowired
	private PageRoleDao pageRoleDao;

	/**
	 * 层次 分组LIST --> 分组内大类 -->大类对应的页面
	 * 
	 * @param req
	 * @param user
	 */
	public void getAllPage(HttpServletRequest req, User user) {
		// 获取所有的有权限的页面大类
		Map<String, Object> param = new HashMap<>();
		param.put("type", "1");
		param.put("roleId", user.getRole());
		// 有权限进入 的大类
		List<Map<String, Object>> classPages = pageRoleDao.retrievePageRoleList(param);
		List<String> allClassName = new ArrayList<>();
		if (classPages != null && classPages.size() > 0) {
			String parentId = "";
			for (Map<String, Object> classPage : classPages) {
				parentId += classPage.get("id");
				parentId += ",";
				allClassName.add(classPage.get("tab_name") + "");
			}
			param.clear();
			param.put("type", "2");
			param.put("roleId", user.getRole());
			param.put("parentId", parentId.substring(0, parentId.length() - 1));
			// 所有有权限的页面
			List<Map<String, Object>> pages = pageRoleDao.retrievePageRoleList(param);
			// 按大类分
			Map<String, List<Map<String, Object>>> classList = new HashMap<>();
			Map<String, Map<String, Object>> classDefaultPage = new HashMap<>();
			Map<String, String> classPageTabId = new HashMap<>();
			Map<String, String> classNameTabId = new HashMap<>();
			for (Map<String, Object> classPage : classPages) {
				List<Map<String, Object>> list = new ArrayList<>();
				String classId = classPage.get("id") + "";
				String className = classPage.get("tab_name") + "";
				String tabId = classPage.get("tab_id") + "";
				for (Map<String, Object> page : pages) {
					String parent = page.get("parent_id") + "";
					String tabIdPage = page.get("tab_id") + "";
					if (StringUtils.equals(classId, parent)) {
						list.add(page);
						classPageTabId.put(tabIdPage, tabId);
					}
				}
				if (list.size() > 0) {
					classList.put(tabId, list);
					classDefaultPage.put(className, list.get(0));
					classNameTabId.put(className, tabId);
				}
			}
			// 大类的名称
			req.getSession().setAttribute("allGroupClassList", allClassName);
			// 大类默认界面
			req.getSession().setAttribute("classDefaultPage", classDefaultPage);
			// 大类对应页面集合
			req.getSession().setAttribute("classList", classList);
			// 页面的tabId对应大类的tabid
			req.getSession().setAttribute("classPageTabId", classPageTabId);
			// 大类对应的tabid
			req.getSession().setAttribute("classNameTabId", classNameTabId);
		}
	}

	public ModelAndView list() {

		Map<String, Object> param = new HashMap<>();
		List<Page> pageListbig = pageRoleDao.retrieveListbig(param);//bas_user_page
		List<Map<String, Object>> allRole = pageRoleDao.retrieveAllRole();//bas_user_role  id  role_name
		for (Page page : pageListbig) {
			param.clear();
			Long tabid=page.getId();
			param.put("tabId", tabid);
			param.put("id", page.getId());
			List<Map<String, Object>> tabList = pageRoleDao.retrieveRoleByTab(param);//bas_user_role_page_relat  id  role_id 
			page.setRoleList(tabList);
			int tabid2=Integer.valueOf(page.getTabId());
			int pagenum=pageRoleDao.retrievepagenum(tabid2);
			if(pagenum==2){
				pagenum=1;
			}
			page.setPageNum(pagenum);
		/*	if(tabList != null && tabList.size()>0){
				page.setPageNum(tabList.size()+2);
			}else{
				page.setPageNum(2);
			}*/
		}
		List<Page> pageListsmall = pageRoleDao.retrieveListsmall(param);//bas_user_page
		for (Page page : pageListsmall) {
			param.clear();
			param.put("tabId", page.getId());
			param.put("parentId", page.getParentId());
			List<Map<String, Object>> tabList = pageRoleDao.retrieveRoleByTab(param);//bas_user_role_page_relat  id  role_id 
			page.setRoleList(tabList);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("listbig", pageListbig);
		map.put("listsmall", pageListsmall);
		map.put("allRole", allRole);
		return new ModelAndView("management/index", map);
	}

	public ReturnData addRole(String id, String role, String tabId) {
		// 判断是否有重复的role
		Map<String, Object> param = new HashMap<>();
		param.put("tabId", tabId);
		if(StringUtils.isNotBlank(role)){
			param.put("roleId", role);
		}else{
			param.put("roleId", -1);
		}
		List<Long> conflict = pageRoleDao.getRoleConflict(param);
		if (conflict != null && conflict.size() > 0) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc("role重复");
			return ReturnUtil.fail(rc);
		}
		// 修改
		if (StringUtils.isNotBlank(id)) {
			if (StringUtils.isNotBlank(role)) {
				pageRoleDao.updateRole(param);
			} else {
				// 删除
				pageRoleDao.deleteRelatById(Long.parseLong(id));
			}
		} else {
			// 新增
			param.clear();
			param.put("roleId", role);
			param.put("tabId", tabId);
			pageRoleDao.insertRoleRelat(param);
		}
		return ReturnUtil.success();
	}

	public void initRole() {
		// 初始化权限
		Map<Integer, String> roleMap = RoleID.getRoleMap();
		List<Map<String, Object>> allRole = pageRoleDao.retrieveAllRole();
		Map<Integer, String> roleAll = new HashMap<>();
		for (Map<String, Object> role : allRole) {
			roleAll.put(Integer.parseInt(role.get("id") + ""), role.get("role_name") + "");
		}
		for (Integer key : roleMap.keySet()) {
			if(roleAll.get(key) == null && key != -1){
				//新增role
				Map<String , Object> param = new HashMap<>();
				param.put("id", key);
				param.put("roleName", roleMap.get(key));
				param.put("remark", "");
				pageRoleDao.insertRole(param);
			}
		}
	}

}
